﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Windows.Interactivity;

namespace RIA.NTiersApp.Extensions
{
    public class DoubleClickTrigger : TriggerBase<UIElement>
    {
        private readonly DispatcherTimer timer;

        public DoubleClickTrigger ()
        {
            timer = new DispatcherTimer
            {
                Interval = new TimeSpan ( 0, 0, 0, 0, 200 )
            };

            timer.Tick += OnTimerTick;

        }

        protected override void OnAttached ()
        {
            base.OnAttached ();
            AssociatedObject.MouseLeftButtonDown += OnMouseButtonDown;
        }

        protected override void OnDetaching ()
        {
            base.OnDetaching ();
            AssociatedObject.MouseLeftButtonDown -= OnMouseButtonDown;
            if ( timer.IsEnabled )
                timer.Stop ();
        }

        private void OnMouseButtonDown ( object sender, MouseButtonEventArgs e )
        {
            if ( !timer.IsEnabled )
            {
                timer.Start ();
                return;
            }
            timer.Stop ();
            InvokeActions ( null );
        }

        private void OnTimerTick ( object sender, EventArgs e )
        {
            timer.Stop ();
        }

    }
}
